<template>
  <div>
    <!--我的界面 -->
    <div class="My_title__39w3V">
      <!-- 成功背景图片 -->
      <van-image
        class="My_bg__2j-VX"
        src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202004%2F25%2F20200425173132_svsej.thumb.1000_0.jpeg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1661862189&t=14f99fafec924879e6596cc199421b9e"
        v-if="isLogin"
      />
      <!-- 游客背景图片 -->
      <van-image
        class="My_bg__2j-VX"
        src="http://liufusong.top:8080/img/profile/bg.png"
        v-else
      />

      <!-- 登录头像界面 -->
      <div class="My_info__eOYeg" v-if="isLogin">
        <div class="My_myIcon__3cKIV">
          <van-image
            round
            class="My_avatar__2Fbh7"
            src="https://img0.baidu.com/it/u=4060770951,4069855872&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"
          />
        </div>
        <div class="My_user__B6O1D">
          <div class="My_name__3U2NB">哈哈</div>
          <div class="My_edit__3wqlv">
            <van-button type="primary" size="small" round @click="logout">
              &nbsp;&nbsp; 退出 &nbsp;&nbsp;
            </van-button>
            <p>编辑个人资料</p>
          </div>
        </div>
      </div>
      <!-- 游客头像界面 -->
      <div class="My_info__eOYeg" v-else>
        <div class="My_myIcon__3cKIV">
          <van-image
            class="My_avatar__2Fbh7"
            src="http://liufusong.top:8080/img/profile/avatar.png"
          />
        </div>
        <div class="My_user__B6O1D">
          <div class="My_name__3U2NB">游客</div>
          <div class="My_edit__3wqlv">
            <van-button type="primary" size="small" to="/login"
              >去登录</van-button
            >
          </div>
        </div>
      </div>
    </div>
    <!-- 图标区域 -->
    <div>
      <van-grid clickable :column-num="3">
        <van-grid-item
          icon="like-o"
          text="我的收藏"
          :to="isLogin ? '/collection' : '/login'"
        />
        <van-grid-item
          icon="shopping-cart-o"
          text="我的出租"
          :to="isLogin ? '/management' : '/login'"
        />
        <van-grid-item icon="todo-list-o" text="看房记录" />
        <van-grid-item icon="vip-card-o" text="成为房主" />
        <van-grid-item icon="user-o" text="个人资料" />
        <van-grid-item icon="phone-o" text="联系我们" />
      </van-grid>
    </div>
    <!-- 底部banner -->
    <div class="My_ad__1wZZj">
      <img src="http://liufusong.top:8080/img/profile/join.png" />
    </div>
  </div>
</template>

<script>
import { UserInfoAPI } from "@/api";

export default {
  name: "My",
  data() {
    return {
      userInfo: {},
    };
  },
  created() {
    this.getUserInfo();
  },
  methods: {
    // 获取用户登录信息
    async getUserInfo() {
      const {
        data: { body },
      } = await UserInfoAPI();
      // console.log(res);
      this.userInfo = body;
    },
    logout() {
      // 退出提示
      this.$dialog
        .confirm({
          title: "提示",
          message: "是否确定退出",
        })
        .then(() => {
          // on confirm
          // 确认后置空token 并跳转至登录页
          this.$store.commit("user/SET_TOKEN", "");
          this.$router.push("/login");
        })
        .catch(() => {
          // on cancel
          // 取消什么也不做
          return;
        });
    },
  },
  computed: {
    // 根据token动态显示登录和未登录界面的展示
    isLogin() {
      return !!this.$store.state.user.token;
    },
  },
};
</script>

<style>
.My_title__39w3V {
  min-height: 375px;
  position: relative;
}
.My_bg__2j-VX {
  width: 100%;
}
.My_info__eOYeg {
  position: absolute;
  background: #fff;
  width: 75%;
  height: 55%;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  box-shadow: 0 0 10px 3px #ddd;
  margin: 50px auto 10px;
  padding: 0 20px;
  text-align: center;
  font-size: 13px;
}
.My_info__eOYeg .My_edit__3wqlv p {
  margin-top: 10px;
  font-size: 12px;
  color: #999;
}
.My_myIcon__3cKIV {
  position: relative;
  transform: translateY(-50%);
  border-radius: 50%;
  width: 70px;
  height: 70px;
  border: 5px solid #f5f5f5;
  display: inline-block;
  box-shadow: 0 2px 2px #bdbdbd;
}
.My_avatar__2Fbh7 {
  width: 100%;
  border-radius: 50%;
}
.My_user__B6O1D {
  padding-top: 15px;
}
.My_name__3U2NB {
  margin-top: -30px;
  margin-bottom: 10px;
}
.My_ad__1wZZj {
  text-align: center;
  margin-top: 10px;
}
.My_ad__1wZZj img {
  width: 92%;
}
</style>
